package org.fastiot.data

import org.fastiot.protocol.Command.Aging

class RemoteControl {

    static class Command {
        String symbol
        String label
        Aging aging
        String metadata
        String contents
        byte[] encodedContents

        static constraints = {
            symbol(blank: false)
            label(blank: false)
            aging(nullable: false, inList: Aging.values().toList())
            metadata(blank: false)
            contents(blank: false)
            encodedContents(nullable: false)
        }

        static mapping = {
            metadata type: "text"
            contents type: "text"
        }
    }

    static class Response {
        static enum Result {
            FAILURE(0), SUCCESS(1)

            private int value
            Result(int value) { this.value = value }
        }

        Result result
        Date dateResponded
        String contents

        static constraints = {
            result(nullable: true)
            dateResponded(nullable: true)
            contents(nullable: true)
        }

        static mapping = {
            contents type: "text"
        }
    }

    static class Operation {
        Date dateRequested
        String operator
        Integer redoCount
        Date lastRedone

        static constraints = {
            operator(blank: false)
            dateRequested(nullable: false)
            redoCount(nullable: true, min: 0)
            lastRedone(nullable: true)
        }
    }

    static mapWith = "mongo"
    static embedded = ['command', 'operation', 'response']

    static constraints = {
        terminalCode(blank: false)
        command(nullable: false)
        operation(nullable: false)
        response(nullable: true)
    }

    static mapping = {
        stateless true
        sort lastUpdated: "desc"
    }

    String id

    String terminalCode
    Command command
    Operation operation
    Response response

    Date dateCreated
    Date lastUpdated
}
